package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import model.Model;
import dao.DBManager;

public class DatabaseView extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JButton jButton = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JTextField hostnameField = null;
	private JTextField databaseField = null;
	private JLabel jLabel3 = null;
	private JTextField userField = null;
	private JLabel jLabel4 = null;
	private JPasswordField passwordField = null;
	private JPanel jPanel2 = null;
	private JComboBox jComboBox = null;
	private ActionListener listener;
	private JLabel jLabel5 = null;
	private JButton connectButton = null;
	private Model model = null;

	/**
	 * This is the default constructor
	 */
	public DatabaseView( Model hetModel ) {
		super();
		this.model = hetModel;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		BorderLayout borderLayout = new BorderLayout();
		borderLayout.setHgap(0);
		borderLayout.setVgap(0);
		jLabel = new JLabel();
		jLabel.setText("Database Instellingen");
		jLabel.setFont(new Font("Dialog", Font.BOLD, 18));
		this.setLayout(borderLayout);
		this.setSize(554, 334);
		this.add(getJPanel(), BorderLayout.SOUTH);
		this.add(getJPanel1(), BorderLayout.CENTER);
		this.add(getJPanel2(), BorderLayout.NORTH);
		listener = new Luisteraar();
		jComboBox.addActionListener(listener);
		jButton.addActionListener(listener);

		loadProperties("DEFAULT");
	}

	private class Luisteraar implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			if (e.getSource().equals(jComboBox)) {
				JComboBox sender = (JComboBox) e.getSource();
				String item = sender.getSelectedItem().toString();
				String driver = "";
				driver = item;
				loadProperties(driver);
			} else if (e.getSource().equals(jButton)) {
				System.out.println("Apply button pressed");
				saveProperties();
			}
		}
	}

	private void loadProperties(String driver) {

		Properties props = new Properties();
		FileInputStream in;
		String hostname = "";
		String database = "";
		String username = "";
		String password = "";
		String drivers = "";

		try {
			in = new FileInputStream("database.properties");
			props.load(in);
			in.close();

			drivers = props.getProperty("jdbc.drivers");
			if (drivers != null) {
				hostname = "localhost";
				database = props.getProperty("jdbc.database");
				username = props.getProperty("jdbc.username");
				password = props.getProperty("jdbc.password");
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (driver.equalsIgnoreCase("DEFAULT")) {
			jComboBox.setSelectedItem("MySQL");
			hostnameField.setText(hostname);
			databaseField.setText(database);
			userField.setText(username);
			passwordField.setText(password);
		}
	}

	private void saveProperties() {
		Properties props = new Properties();
		FileOutputStream out;
		String hostname = "";
		String database = databaseField.getText();
		String username = userField.getText();
		String password = passwordField.getText();
	
		String driver = "";
		String drivers = (String) jComboBox.getSelectedItem();
		if (drivers.equalsIgnoreCase("mysql")) {
			driver = "com.mysql.jdbc.Driver";
			hostname = "jdbc:mysql://" + hostnameField.getText();
		} else if (drivers.equalsIgnoreCase("postgresql")) {
			driver = "org.postgresql.Driver";
			hostname = "jdbc:postgresql://" + hostnameField.getText();
		} else if (drivers.equalsIgnoreCase("derby")) {
			driver = "org.apache.derby.jdbc.EmbeddedDriver";
			hostname = "jdbc:derby";
		}

		try {
			out = new FileOutputStream("database.properties");

			props.setProperty("jdbc.drivers", driver);
			props.setProperty("jdbc.hostname", hostname);
			props.setProperty("jdbc.database", database);
			props.setProperty("jdbc.username", username);
			props.setProperty("jdbc.password", password);

			props.store(out, null);
			out.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 1;
			gridBagConstraints11.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = -1;
			gridBagConstraints.gridy = -1;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jPanel.add(getJButton(), gridBagConstraints);
			jPanel.add(getConnectButton(), gridBagConstraints11);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.anchor = GridBagConstraints.WEST;
			gridBagConstraints10.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints10.gridy = 4;
			jLabel5 = new JLabel();
			jLabel5.setText("Databasename");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 1;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.gridx = 1;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.fill = GridBagConstraints.NONE;
			gridBagConstraints8.gridy = 6;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints8.gridx = 0;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints9.gridy = 6;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.gridx = 1;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 1;
			gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.gridy = 5;
			jLabel4 = new JLabel();
			jLabel4.setText("password");
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.NONE;
			gridBagConstraints6.gridy = 5;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints6.gridx = 0;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 1;
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridy = 4;
			jLabel3 = new JLabel();
			jLabel3.setText("username");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 2;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridx = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints3.fill = GridBagConstraints.NONE;
			gridBagConstraints3.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("Hostname");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints1.fill = GridBagConstraints.NONE;
			gridBagConstraints1.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Driver");
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jPanel1.add(jLabel1, gridBagConstraints1);
			jPanel1.add(jLabel2, gridBagConstraints3);
			jPanel1.add(getHostnameField(), gridBagConstraints4);
			jPanel1.add(getDatabaseField(), gridBagConstraints5);
			jPanel1.add(jLabel3, gridBagConstraints6);
			jPanel1.add(getUserField(), gridBagConstraints7);
			jPanel1.add(jLabel4, gridBagConstraints8);
			jPanel1.add(getPasswordField(), gridBagConstraints9);
			jPanel1.add(getJComboBox(), gridBagConstraints2);
			jPanel1.add(jLabel5, gridBagConstraints10);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Apply");
		}
		return jButton;
	}

	/**
	 * This method initializes hostnameField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getHostnameField() {
		if (hostnameField == null) {
			hostnameField = new JTextField();
		}
		return hostnameField;
	}

	private JTextField getDatabaseField() {
		if (databaseField == null) {
			databaseField = new JTextField();
		}
		return databaseField;
	}

	/**
	 * This method initializes userField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getUserField() {
		if (userField == null) {
			userField = new JTextField();
		}
		return userField;
	}

	/**
	 * This method initializes passwordField
	 * 
	 * @return javax.swing.JPasswordField
	 */
	private JPasswordField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JPasswordField();
		}
		return passwordField;
	}

	/**
	 * This method initializes jPanel2
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jPanel2.add(jLabel, new GridBagConstraints());
		}
		return jPanel2;
	}

	/**
	 * This method initializes jComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.addItem("MySQL");
			jComboBox.addItem("Postgresql");
			jComboBox.addItem("Derby");
		}
		return jComboBox;
	}

	/**
	 * This method initializes connectButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getConnectButton() {
		if (connectButton == null) {
			connectButton = new JButton();
			connectButton.setText("Connect");
			connectButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
					// TODO Auto-generated Event stub actionPerformed()
					try {
						Connection conn = DBManager.getInstance().getConnection();
						model.setConnected(true);
					} catch (SQLException e1) {
						System.err.println("Geen verbinding kunnen maken");
						model.setConnected(false);
					}
				}
			});
		}
		return connectButton;
	}

} // @jve:decl-index=0:visual-constraint="83,53"
